package com.core.cloudcommon;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.UUID;

/**
 * @author =====Lqj=====
 * @className CommonCustomException
 * @description 自定义异常
 * @create 2024/11/15 13:46
 **/
@Getter
@Setter
@Slf4j
public class CommonCustomException extends RuntimeException {


    /**
     * 错误码
     */
    @JsonProperty("code")
    private Integer code;

    /**
     * 错误消息
     */
    @JsonProperty("message")
    private String message;

    /**
     * 标志
     */
    @JsonProperty("success")
    private boolean success = false;

    /**
     * 时间戳
     */
    @JsonProperty("timestamp")
    private Long timestamp = System.currentTimeMillis();

    /**
     * 透传ID
     */
    @JsonProperty("requuid")
    public String requuid = UUID.randomUUID().toString().replace("-", "");

    public CommonCustomException() {
        super();
    }

    public CommonCustomException(Integer code, String message, Throwable cause) {
        super(code + message, cause);
        log.error("透传ID：" + this.requuid + "\n错误编码：" + code + "\n错误描述：" + message);
        this.code = code;
        this.message = message;
    }

    public CommonCustomException(Integer code, String message) {
        super(code + message, null);
        log.error("\n透传ID：" + this.requuid + "\n错误编码：" + code + "\n错误描述：" + message);
        // 获取当前堆栈信息
        StackTraceElement[] stackTrace = this.getStackTrace();
        if (stackTrace.length > 0) {
            StackTraceElement element = stackTrace[0];
            log.error("\n透传ID：" + this.requuid + " ------ 代码坐标：" + element.getClassName() +
                   "\n"+ element.getFileName() +
                    element.getLineNumber());
        }
        this.code = code;
        this.message = message;

    }
}
